//
//  AuthDetainmentView.swift
//  认证 - 挽留弹窗
//
//  Created by 千策万划-mini on 2025/5/12.
//

import Foundation

class AuthDetainmentView: UIView {
    
    var blockConfirmExit:(() -> Void)?
    
    // MARK: - lazy
    lazy var viewBg: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(355, 349)
        result.isUserInteractionEnabled = true
        result.image = UIImage(named: "icon_detainment_bg")
        return result
    }()
    lazy var btnClose: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(40+16*2, 40+30*2)
        result.tag = 1
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelTitle: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Leave?",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 24),
            shadowColor: UIColor(hex: "#4105A0"),
            outlineColor: UIColor(hex: "#3B0B9A")
        )
        return result
    }()
    lazy var viewContent: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_login_input_bg")
        result.widthHeight = XY(275, 157)
        return result
    }()
    lazy var labelContent: UILabel = {
        let result = UILabel()
        result.textAlignment = .center
        result.font = UIFont.customRegularFont(size: 14)
        result.textColor = COLOR_LABEL()
        result.numberOfLines = 0
        return result
    }()
    lazy var btnExit: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(132, 46)
        result.setImage(UIImage(named: "icon_pop_confirm"), for: .normal)
        result.tag = 2
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelExit: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Confirm Exit",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#2D0084"),
            outlineColor: UIColor(hex: "#2D0084")
        )
        return result
    }()
    lazy var btnCancel: UIButton = {
        let result = UIButton()
        result.widthHeight = XY(132, 46)
        result.setImage(UIImage(named: "icon_pop_cancel"), for: .normal)
        result.tag = 3
        result.addTarget(self, action: #selector(btnClick(_:)), for: .touchUpInside)
        return result
    }()
    lazy var labelCancel: OutlineLabel = {
        let result = OutlineLabel()
        result.resetView(
            title: "Cancel",
            textColor: COLOR_WHITE(),
            font: UIFont.customBoldFont(size: 16),
            shadowColor: UIColor(hex: "#004600"),
            outlineColor: UIColor(hex: "#1B4506")
        )
        return result
    }()
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.backgroundColor = UIColor(hex: "000000", alpha: 0.8)
        self.addSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加
    func addSubView() {
        addSubview(viewBg)
        viewBg.addSubview(btnClose)
        viewBg.addSubview(labelTitle)
        viewBg.addSubview(viewContent)
        viewContent.addSubview(labelContent)
        
        viewBg.addSubview(btnExit)
        btnExit.addSubview(labelExit)
        viewBg.addSubview(btnCancel)
        btnCancel.addSubview(labelCancel)
        
        resetView()
    }
    
    // MARK: - 点击事件
    @objc func btnClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        switch sender.tag {
        case 1:
            hide()
            break
        case 2: // logout
            hide()
            if let block = blockConfirmExit {
                block()
            }
            break
        case 3: // Cancel
            hide()
            break
        default:
            break
        }
    }
    
    // 显示
    func show() {
        if let window = WindowManager.shared.getCurrentWindow() {
            window.endEditing(true)
            // 设置初始状态（比如从底部开始）
            alpha = 0
            viewBg.transform = CGAffineTransform(translationX: 0, y: 50)
            window.addSubview(self)
            // 添加弹出动画
            UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
                self.alpha = 1
                self.viewBg.transform = .identity
            }
        }
    }

    // 隐藏
    func hide() {
        // 添加消失动画
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn) {
            self.alpha = 0
            self.viewBg.transform = CGAffineTransform(translationX: 0, y: 50)
        } completion: { _ in
            self.removeFromSuperview()
            // 重置 transform，以便下次显示
            self.viewBg.transform = .identity
        }
    }
    
    // MARK: - 重置界面
    func resetView() {
        viewBg.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
        btnClose.rightTop = XY(viewBg.width, 0)
        labelTitle.centerXTop = XY(viewBg.width/2, 25)
        viewContent.centerXTop = XY(viewBg.width/2, 110)
        
        let str = "Don't leave yet! You are only one step\naway from successfully obtaining a\nlow-interest loan! After completing the\ncertification, the funds will be credited\nto your account in as fast as 30\nminutes, helping you to easily deal with\nfinancial problems"
        let paragraphStyle = NSMutableParagraphStyle()
        let font = labelContent.font!
        let minLineHeight: CGFloat = font.lineHeight * 0.8
        paragraphStyle.minimumLineHeight = minLineHeight
        paragraphStyle.maximumLineHeight = minLineHeight
        paragraphStyle.alignment = labelContent.textAlignment
        let attributedString = NSAttributedString(
            string: str,
            attributes: [
                .paragraphStyle: paragraphStyle,
                .font: font
            ]
        )
        labelContent.attributedText = attributedString
        labelContent.sizeToFit()
        labelContent.centerXCenterY = XY(viewContent.width/2, viewContent.height/2+2)
        
        btnExit.rightTop = XY(viewBg.width/2-6, viewContent.bottom+15)
        labelExit.centerXCenterY = XY(btnExit.width/2, btnExit.height/2)
        
        btnCancel.leftCenterY = XY(viewBg.width/2+6, btnExit.centerY)
        labelCancel.centerXCenterY = XY(btnCancel.width/2, btnCancel.height/2)
    }
}
